package com.feinno.urcs.group.service;

import com.feinno.urcs.group.GroupConfiguration;
import com.feinno.urcs.group.api.GroupSubscriptionManager;
import com.feinno.urcs.group.entity.GroupInfo;
import com.feinno.urcs.group.entity.GroupListSubscription;
import com.feinno.urcs.group.entity.GroupMemberSubscription;
import com.feinno.urcs.group.entity.GroupSubscribeType;
import com.feinno.urcs.group.siptask.task.GroupInfoNotifyArgs;
import com.feinno.urcs.group.siptask.task.GroupInfoNotifyType;
import com.feinno.urcs.group.siptask.task.GroupListNotifyArgs;
import com.feinno.urcs.types.MobileNo;
import org.helium.framework.task.TaskProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
public class NotifyMembersHelper {

	private static final Logger LOGGER = LoggerFactory.getLogger(NotifyMembersHelper.class);

	public static void notifyGroupInfoToMembers(TaskProducer<GroupInfoNotifyArgs> task, GroupSubscriptionManager subManager, GroupInfo info) throws IllegalAccessException, InstantiationException {
		notifyGroupInfoToMembers(task, subManager, info, null, GroupInfoNotifyType.DEFAULT, false, null);
	}

	/**
	 * @param info
	 * @param exceptMobile 被排除的人, 不给此人发通知
	 * @param type         通知类型
	 * @param isDismiss    是否为解散群, 解散群时订阅有效期为0
	 * @param quitMobile   退出(被删除)的人
	 */

	public static void notifyGroupInfoToMembers(TaskProducer<GroupInfoNotifyArgs> task, GroupSubscriptionManager subManager, GroupInfo info, MobileNo exceptMobile, GroupInfoNotifyType type, boolean isDismiss, MobileNo quitMobile) throws InstantiationException, IllegalAccessException {
		List<GroupMemberSubscription> subs = subManager.getMemberConferenceSubscriptions(info.getGroupId());
		LOGGER.info(">>>sendNotifyToMember groupId={} members={}", info.getGroupId(), subs.size());
		for (GroupMemberSubscription sub : subs) {
			if (sub.getUserMobile().equals(exceptMobile)) {
				LOGGER.info("Generate InfoNotifyTask group {}  except {}", info.getGroupId(), sub.getUserMobile());
				continue;
			}

			// 只针对本类型的群订阅执行
			if (!sub.getSubType().equals(GroupSubscribeType.AD_GROUP)) {
				continue;
			}
			LOGGER.info("Generate InfoNotifyTask to {}:{}", info.getGroupId(), sub.getUserMobile());
			GroupInfoNotifyArgs args = new GroupInfoNotifyArgs();
			args.setGroupInfo(info);
			args.setToMemberMobileNo(sub.getUserMobile());
			args.setFromTag(sub.getFromTag());
			args.setToTag(sub.getToTag());
			args.setCallId(sub.getCallId());
			args.setCseq(subManager.increateConferenceCseq(info.getGroupId(), sub.getUserMobile()));
			args.setNotifyType(type);
			args.setContact(sub.getContact());
			args.setRoute(sub.getRoute());
			if (isDismiss) {
				args.setExpired(0);
				args.setIsDismiss(true);
			} else {
				args.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
			}
			if (GroupInfoNotifyType.DELETED.equals(type)) {
				args.setQuitMember(quitMobile);
			}
			task.produce(args);
		}
	}

	public static void notifyGroupListToMember(TaskProducer<GroupListNotifyArgs> listNotifyTask, GroupSubscriptionManager subscriptionManager, GroupInfo info, MobileNo toMemberMobile) throws InstantiationException, IllegalAccessException {
		GroupListSubscription sub = subscriptionManager.getGroupListSubscription(toMemberMobile);
		if (sub != null) {
			GroupListNotifyArgs args = new GroupListNotifyArgs();
			args.setFromTag(sub.getFromTag());
			args.setToMemberMobileNo(toMemberMobile);
			args.setToTag(sub.getToTag());
			args.setCallId(sub.getCallId());
			args.setCseq(sub.getCSeq());
			args.setContact(sub.getContact());
			args.setExpired(GroupConfiguration.INSTANCE.getSubscriptionExpires());
			args.setGroupInfo(info);
			args.setRoute(sub.getRoute());
			listNotifyTask.produce(args);
		}
	}
}
